<!-- 供应链管理-上游企业 -->
<template>
  <div>
    <Form ref="form" @ok="search()"></Form>
    <ZgNewPage
      ref="page"
      :showInlineBtn="false"
      :uniqueName="uniqueName"
      :pageOpt="pageOpt"
      :dataBtns="dataBtns"
      :extraBtns="extraBtns"
      :headers="headers"
      :conditions="conditions"
      :adminWidth="240"
      :scrollX="1500"
      @search="onSearch"
      @filter="onFilter"
      @header="mergeHeader"
      @admin="toAdmin"
      @extra="toExtra"
    >
      <template slot="columns">
        <a-table-column
          v-for="(h,i) in headers"
          :key="`column${i}`"
          :width="h.width===0 ? 'auto' : h.width"
          align="center"
          :title="h.label">
          <template slot-scope="record">
            {{ record[h.code] }}
          </template>
        </a-table-column>
      </template>
    </ZgNewPage>
  </div>
</template>
<script>
import Form from './Form'
export default {
  components: {
    Form
  },
  data () {
    return {
      pageTitle: '融资企业管理',
      uniqueName: 'test',
      pageOpt: {
        showPageTitle: false,
        showConditionBtn: true,
        showHeaderConfig: false,
        showAttachBtn: false
      },
      conditions: [
        { code: 'name', compType: '单行文本', label: '信用代码', defaultVal: '', extra: { placeholder: '模糊搜索' } },
        { code: 'name', compType: '单行文本', label: '联系人', defaultVal: '', extra: { placeholder: '模糊搜索' } },
        { code: 'name', compType: '单行文本', label: '联系电话', defaultVal: '', extra: { placeholder: '模糊搜索' } },
        { code: 'name', compType: '单行文本', label: '账户', defaultVal: '', extra: { placeholder: '请输入账户' } },
        { code: 'name', compType: '单行文本', label: '企业名称', defaultVal: '', extra: { placeholder: '模糊搜索' } }
      ],
      headers: [
        { code: '1', label: '信用代码', isShow: true, sort: 0 },
        { code: '2', label: '企业名称', isShow: true, sort: 0 },
        { code: '3', label: '账户', isShow: true, sort: 0 },
        { code: '4', label: '联系人', isShow: true, sort: 0 },
        { code: '5', label: '联系电话', isShow: true, sort: 0 },
        { code: '6', label: '启用状态', isShow: true, sort: 0 }
      ],
      dataBtns: [
        { key: 1, name: '详情', type: 'link', isShow: () => { return true } },
        { key: 2, name: '编辑', type: 'link', isShow: () => { return true } },
        { key: 3, name: '删除', type: 'link', isShow: () => { return true } }
      ],
      extraBtns: [
        { key: 1, name: '新增', icon: 'plus-circle', type: 'primary' }
      ]
    }
  },
  methods: {
    search () {
      const pageRef = this.$refs.page
      if (pageRef) {
        pageRef.search()
      } else {
        setTimeout(() => {
          this.search()
        }, 50)
      }
    },
    onSearch (param) {
      const diyHeaders = localStorage.getItem(this.currentUser.username + '_diyHeader_' + this.uniqueName)
      if (diyHeaders) {
        this.mergeHeader(JSON.parse(diyHeaders))
      }
      // 加载表头
      const list = [
        { key: '1', id: '1', '2': '山西焦煤集团', '1': '91141181699136277J', '34': '张三', '3': 18882984324, '5': new Date().getTime(), '6': '启用' }
      ]
      this.$refs.page.endSearch({
        total: list.length,
        list: list
      })
    },
    toAdmin (opt) {
      if (opt.key === 1) {
        this.$refs.form.open('detail', opt.obj)
      } else if (opt.key === 2) {
         this.$refs.form.open('edit', opt.obj)
      } else if (opt.key === 3) {
         this.confirm(
          () => {
            // this.$http.post('/zg-system/attach/del', [param.obj.id], () => {
            //   this.$refs.page.search()
            // })
          },
        '确定要删除这条数据么')
      }
    },
    toExtra (opt) {
      if (opt.key === 1) {
        this.$refs.form.open('add')
      }
    },
    onFilter (f, param) {
    },
    mergeHeader (newHeaders) {
      if (!newHeaders) {
        return
      }
      this.headers = newHeaders.filter(t => {
        return t.isShow
      }).sort((a, b) => {
        return a.sort - b.sort
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
